package org.example.test.controller;

import org.example.test.model.CategoryModel;
import org.example.test.response.CommonResponse;
import org.example.test.response.ResponseBuilder;
import org.example.test.service.CategoriesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/categories")
public class CategoriesController {
    // 日志记录器
    private static final Logger LOGGER = LoggerFactory.getLogger(CategoriesController.class);

    // 分类服务层依赖注入
    @Autowired
    private CategoriesService categoriesService;

    /**
     * 创建商品分类
     * @param categoryRequest 分类请求数据（包含分类名称等）
     * @return 操作结果（成功无数据返回）
     * @apiNote 需要管理员权限
     */
    @PostMapping("/create")
    public CommonResponse createCategory(@RequestBody CategoryModel categoryRequest) {
        try {
            LOGGER.info("Create category request: {}", categoryRequest);
            // 调用服务层创建分类
            categoriesService.createCategory(categoryRequest);
            return ResponseBuilder.success();
        } catch (Exception e) {
            LOGGER.error("Error creating category", e);
            return ResponseBuilder.error(e.getMessage());
        }
    }

    /**
     * 更新分类信息
     * @param categoryRequest 分类更新数据（需包含分类ID）
     * @return 操作结果
     * @apiNote 分类名称需保证唯一性
     */
    @PutMapping("/update")
    public CommonResponse updateCategory(@RequestBody CategoryModel categoryRequest) {
        try {
            LOGGER.info("Update category request - categoryId: {}", categoryRequest.getId());
            // 调用服务层更新分类
            categoriesService.updateCategory(categoryRequest);
            return ResponseBuilder.success();
        } catch (Exception e) {
            LOGGER.error("Error updating category", e);
            return ResponseBuilder.error(e.getMessage());
        }
    }

    /**
     * 删除分类
     * @param categoryId 分类ID
     * @return 操作结果
     * @apiNote 如果分类下有商品存在，应禁止删除
     */
    @DeleteMapping("/delete")
    public CommonResponse deleteCategory(@RequestParam Long categoryId) {
        try {
            LOGGER.info("Delete category request - categoryId: {}", categoryId);
            // 调用服务层删除分类
            categoriesService.deleteCategory(categoryId);
            return ResponseBuilder.success();
        } catch (Exception e) {
            LOGGER.error("Error deleting category", e);
            return ResponseBuilder.error(e.getMessage());
        }
    }

    /**
     * 获取单个分类详情
     * @param categoryId 分类ID
     * @return 分类详细信息
     */
    @GetMapping("/get")
    public CommonResponse getCategory(@RequestParam Long categoryId) {
        try {
            LOGGER.info("Get category request - categoryId: {}", categoryId);
            // 查询分类详情
            CategoryModel category = categoriesService.getCategoryById(categoryId);
            return ResponseBuilder.success(category);
        } catch (Exception e) {
            LOGGER.error("Error getting category", e);
            return ResponseBuilder.error(e.getMessage());
        }
    }

    /**
     * 获取全部分类列表
     * @return 分类列表（按创建时间排序）
     * @apiNote 用于前台分类导航或后台管理
     */
    @GetMapping("/list")
    public CommonResponse listCategories() {
        try {
            LOGGER.info("List all categories request");
            // 查询全部分类
            List<CategoryModel> categories = categoriesService.getAllCategories();
            return ResponseBuilder.success(categories);
        } catch (Exception e) {
            LOGGER.error("Error listing categories", e);
            return ResponseBuilder.error(e.getMessage());
        }
    }
}

